<template>
  <div style="height: 100vh; display: flex; align-items: center; justify-content: center; background-color: #0f9876;">
    <div style="display: flex; background-color: white; width: 50%; border-radius: 5px; overflow: hidden;">
      <div style="flex: 1;">
        <img src="@/assets/logo.jpg" alt="" style="width: 100%; justify-content: center;">
      </div>
      <div style="flex: 1; display: flex; align-items: center;">
        <el-form :model="user" style="width: 85%;" :rules="rules" ref="loginRef">
          <div style="font-size: 20px; font-weight: bold; text-align: center;">欢迎登录后台管理系统</div>
          <el-form-item prop="username" style="margin-top: 20px;">
            <el-input prefix-icon="el-icon-user" size="medium" placeholder="请输入账号" v-model="user.username"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input prefix-icon="el-icon-lock" size="medium" show-password placeholder="请输入密码"
              v-model="user.password"></el-input>
          </el-form-item>
          <el-form-item prop="code">
            <div style="display: flex;">
              <el-input prefix-icon="el-icon-circle-check" size="medium" style="flex: 1;" placeholder="请输入验证码"
                v-model="user.code"></el-input>
              <div style="flex: 1;">
                <valid-code @input="getCode" />
              </div>
            </div>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" style="width: 100%;" @click="login">登 录</el-button>
          </el-form-item>
          <div style="display: flex;">
            <div style="flex: 1;">还没有账号？请 <span style="color:#0f9876; cursor: pointer;" @click="register">注册</span></div>
            <div style="flex: 1; text-align: right;"><span style="color: #0f9876; cursor: pointer;">忘记密码</span></div>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import ValidCode from '@/components/ValidCode.vue';
export default {
  components: { ValidCode },
  name: "Login",
  comments: { ValidCode },
  data() {

    // 验证码校验
    const validateCode = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入验证码'));
      } else if (value.localeCompare(this.code, undefined, { sensitivity: 'base' }) !== 0) {
        callback(new Error('验证码输入有误'));
      } else {
        callback();
      }
    }

    return {
      code: '', // 验证码组件传递过来的code
      user: {
        code: '', // 表单里用户输入的code
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        code: [
          { validator: validateCode, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    getCode(code) {
      // console.log(code)
      this.code = code;
    },
    login() {
      // console.log(this.code);
      this.$refs['loginRef'].validate((valid) => {
        if (valid) {
          // 验证通过
          this.$request.post('/admin/admin/login', this.user).then(res => {
            console.log(res.code)
            if (res.code === 1) {
              this.$router.push('/')
              this.$message.success('登录成功')
              console.log(res.data)
              localStorage.setItem('user', JSON.stringify(res.data))
            } else {
              this.$message.error('登录失败')
            }
          })
        }
      })
    },
    register(){
      console.log('注册')
      this.$router.push('/register')
    }
  }
}
</script>

<style></style>